package projbd;

import java.io.IOException;
import java.sql.Statement;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Stack;

/**
 *
 * @author Matheus
 */
public class XMLReconstructor {
    
    private Writer xmlWriter;
    private String orderStatement;
    private Statement statement;
    private Connection connection;
    private Stack<String> tagStack = new Stack<String>();
    
    public XMLReconstructor(String file) throws UnsupportedEncodingException, FileNotFoundException, SQLException, IOException {
                
        connection = DriverManager.getConnection("jdbc:postgresql://localhost:5432/shakespeare", "postgres", "admin");
        statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
                                               ResultSet.CONCUR_UPDATABLE);
        xmlWriter = new OutputStreamWriter(new FileOutputStream(file), "UTF-8");
        orderStatement = "SELECT b.value AS bvalue, b.attributes AS battributes, b.datatype AS bdatatype, a.value AS avalue FROM dewey_edges AS a RIGHT JOIN dewey_edges AS b ON (b.father_id = a.dewey) ORDER BY b.order_rec";
    }
    
    public void reconstruct() throws SQLException, IOException {
        ResultSet result = statement.executeQuery(orderStatement);
        result.first();
        while (!result.isAfterLast()) {
            String value = result.getString("bvalue");
            String attributes = result.getString("battributes");
            if (attributes != null) attributes = attributes.trim();
            String datatype = result.getString("bdatatype");
            
            if (datatype.equals("tag")) {
               tagStack.push(value);
                if (attributes != null) {
                    xmlWriter.write("<" + value + " " + attributes + ">\n");
                } else {
                    xmlWriter.write("<" + value + ">\n");
                }
            } else if (datatype.equals("text")) {
                xmlWriter.write(value + "\n");
                String fatherValue;
                if (result.isLast()) {
                    fatherValue = result.getString("avalue");
                } else {
                    result.next();
                    fatherValue = result.getString("avalue");
                    result.previous();
                }
                
                while (!(tagStack.peek()).equals(fatherValue)) {
                    String stackValue = tagStack.pop();
                    if (attributes != null) {
                        xmlWriter.write("</" + stackValue + " " + attributes + ">\n");
                    } else {
                        xmlWriter.write("</" + stackValue + ">\n");
                    }
                }

                if (result.isLast()) {
                    String sValue = tagStack.pop();
                    if (attributes != null) {
                        xmlWriter.write("</" + sValue + " " + attributes + ">\n");
                    } else {
                        xmlWriter.write("</" + sValue + ">\n");
                    }
                }
            }
            result.next();
        }
        
        while(!tagStack.empty())
            xmlWriter.write("</" + tagStack.pop() + ">\n");
        
        xmlWriter.close();
    }
}
